#!/bin/bash

Proxy_ProjectDir=/usr/local/nginx/conf/proxy
ScriptDir=/usr/local/src/nginx-install/config
BagDir=/usr/local/src/nginx-install
NginxConfig=/usr/local/nginx/conf

IP_LIST01=192.168.8.10
#IP_LIST02=192.168.8.11
#IP_SERVER=192.168.8.12


# ------------------------------------------- install nginx ------------------------------------------- #
function Install_Nginx() {
    yum install -y wget make cmake gcc gcc-c++ 
    yum install -y pcre-devel lib zlib-devel openssl openssl-devel
    if [ $? -eq 0 ];then
        echo -e '\e[32mSuccessful!\e[0m'
    else
        echo -e '\e[31mFailed\e[0m'
        exit 0
    fi
    echo -e '\e[31mInstallation nginx\e[0m'

    cd /usr/local/src/nginx-install && tar -zxvf openssl-1.1.1g.tar.gz && tar -zxvf nginx-1.18.0.tar.gz  && cd nginx-1.18.0

    ./configure  --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module \
                --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_gzip_static_module \
                --with-http_sub_module --with-stream --with-stream_ssl_module \
                --with-openssl=/usr/local/src/nginx-install/openssl-1.1.1g --with-openssl-opt='enable-weak-ssl-ciphers'
    make && make install
    if [ $? -eq 0 ];then
        echo -e '\e[32minstall Successful!\e[0m'
    else
        echo -e '\e[31mFailed\e[0m'
        exit 0
    fi

    mkdir -p /data/wwwlogs/

    # 创建虚拟主机目录
    mkdir /usr/local/nginx/conf/vhost
    mkdir /usr/local/nginx/conf/proxy

    # 软连接，直接使用nginx -s stop|reload
    ln -sf /usr/local/nginx/sbin/nginx /usr/bin/nginx
    cp -r ${ScriptDir}/nginx.conf        ${NginxConfig}/nginx.conf
    cp -r ${ScriptDir}/enable-php.conf   ${NginxConfig}/enable-php.conf 

    useradd www

    nginx -t
    if [ $? -eq 0 ];then
        echo -e  ${ip_list} '\e[32mTest Successful ready to start!\e[0m'
    else
        echo -e '\e[31mFailed\e[0m'
        exit 0
    fi
    nginx
}


# ------------------------------------------- 生成并修改配置文件 ------------------------------------------- #
function Sed_config01() {
    # 第一层负载
    echo "# --------- ${Parameter01}-${i} --------- #" >> ${Proxy_ProjectDir}/${Parameter01}.conf
    cat ${ScriptDir}/proxy.conf >> ${Proxy_ProjectDir}/${Parameter01}.conf
    # 项目名
    sed -i 's@'ProxyName'@'${Parameter01}-${i}'@g' ${Proxy_ProjectDir}/${Parameter01}.conf
    # 需要转发的ip和地址
    sed -i 's@'ip'@'${IP_SERVER}'@g' ${Proxy_ProjectDir}/${Parameter01}.conf
    # 本地项目端口
    sed -i 's@'port'@'${i}'@g' ${Proxy_ProjectDir}/${Parameter01}.conf

    nginx -t  >/dev/null 2>&1
    if [ $? -eq 0 ];then
        echo -e '\e[32m 本机 Test Successful ready to start!\e[0m'
    else
        echo -e '\e[31mFailed\e[0m'
        echo -e '\e[31mnginx -t检测失败，请检查配置文件语法\e[0m'
        exit 0
    fi

    netstat -nutpl | grep nginx >/dev/null 2>&1
    if [ $? -eq 0 ];then
        nginx -s reload
        # echo -e '\e[32m已为您重载nginx配置文件!\e[0m'
    else
        echo -e '\e[31mnginx没有启动，正在为您重新启动nginx\e[0m'
        nginx
   fi

}

function Sed_config02() {
    # 第二负载
    ssh root@${IP_LIST02}  "echo  # --------- ${Parameter01}-${i} --------- # >> ${Proxy_ProjectDir}/${Parameter01}.conf"
    ssh root@${IP_LIST02}  "cat ${ScriptDir}/proxy-02.conf >> ${Proxy_ProjectDir}/${Parameter01}.conf"
    # 项目名
    ssh root@${IP_LIST02}  "sed -i 's@'ProxyName'@'${Parameter01}-${i}'@g' ${Proxy_ProjectDir}/${Parameter01}.conf"
    # 需要转发的ip和地址
    ssh root@${IP_LIST02}  "sed -i 's@'ip'@'${IP_SERVER}'@g' ${Proxy_ProjectDir}/${Parameter01}.conf"
    # 本地项目端口
    ssh root@${IP_LIST02}  "sed -i 's@'port'@'${i}'@g' ${Proxy_ProjectDir}/${Parameter01}.conf"

    ssh root@${IP_LIST02}  "nginx -t"  >/dev/null 2>&1
    if [ $? -eq 0 ];then
        echo -e ${IP_LIST02} '\e[32mTest Successful ready to start!\e[0m'
    else
        echo -e '\e[31mFailed\e[0m'
        echo -e ${IP_LIST02} '\e[31mnginx -t检测失败，请检查配置文件语法\e[0m'
        exit 0
    fi

    ssh root@${IP_LIST02}  "netstat -nutpl | grep nginx"  >/dev/null 2>&1
    if [ $? -eq 0 ];then
        ssh root@${IP_LIST02}  "nginx -s reload"
        # echo -e '\e[32m已为您重载nginx配置文件!\e[0m'
    else
        echo -e '\e[31mnginx没有启动，正在为您重新启动nginx\e[0m'
        ssh root@${IP_LIST02}  "nginx"
   fi
}






# ------------------------------------------- 相同配置的备用负载节点 ------------------------------------------- #
function Back_proxy01() {

    

    for ip_list in ${IP_BackProxy01}
    do
        scp -r ${ScriptDir}/*  root@${IP_BackProxy01}:${ScriptDir} >/dev/null 2>&1
        ssh root@${ip_list}  "echo  # --------- ${Parameter01}-${i} --------- # >> ${Proxy_ProjectDir}/${Parameter01}.conf"
        ssh root@${ip_list}  "cat ${ScriptDir}/proxy-02.conf >> ${Proxy_ProjectDir}/${Parameter01}.conf"
        # 项目名
        ssh root@${ip_list}  "sed -i 's@'ProxyName'@'${Parameter01}-${i}'@g' ${Proxy_ProjectDir}/${Parameter01}.conf"
        # 需要转发的ip和地址
        ssh root@${ip_list}  "sed -i 's@'ip'@'${IP_SERVER}'@g' ${Proxy_ProjectDir}/${Parameter01}.conf"
        # 本地项目端口
        ssh root@${ip_list}  "sed -i 's@'port'@'${i}'@g' ${Proxy_ProjectDir}/${Parameter01}.conf"

        ssh root@${ip_list}  "nginx -t"  >/dev/null 2>&1
        if [ $? -eq 0 ];then
            echo -e ${ip_list} '\e[32mTest Successful ready to start!\e[0m'
            echo ""
        else
            echo -e '\e[31mFailed\e[0m'
            echo -e  ${ip_list} '\e[31mnginx -t检测失败，请检查配置文件语法\e[0m'
            exit 0
        fi

        ssh root@${ip_list}  "netstat -nutpl | grep nginx"  >/dev/null 2>&1
        if [ $? -eq 0 ];then
            ssh root@${ip_list}  "nginx -s reload"
            # echo -e '\e[32m已为您重载nginx配置文件!\e[0m'
        else
            echo -e '\e[31mnginx没有启动，正在为您重新启动nginx\e[0m'
            ssh root@${ip_list}  "nginx"
        fi
    done
}


function Back_proxy02() {

    

    for ip_list in ${IP_BackProxy02}
    do
        scp -r ${ScriptDir}/*  root@${IP_BackProxy02}:${ScriptDir} >/dev/null 2>&1
        ssh root@${ip_list}  "echo  # --------- ${Parameter01}-${i} --------- # >> ${Proxy_ProjectDir}/${Parameter01}.conf"
        ssh root@${ip_list}  "cat ${ScriptDir}/proxy-02.conf >> ${Proxy_ProjectDir}/${Parameter01}.conf"
        # 项目名
        ssh root@${ip_list}  "sed -i 's@'ProxyName'@'${Parameter01}-${i}'@g' ${Proxy_ProjectDir}/${Parameter01}.conf"
        # 需要转发的ip和地址
        ssh root@${ip_list}  "sed -i 's@'ip'@'${IP_SERVER}'@g' ${Proxy_ProjectDir}/${Parameter01}.conf"
        # 本地项目端口
        ssh root@${ip_list}  "sed -i 's@'port'@'${i}'@g' ${Proxy_ProjectDir}/${Parameter01}.conf"

        ssh root@${ip_list}  "nginx -t"  >/dev/null 2>&1
        if [ $? -eq 0 ];then
            echo -e  ${ip_list} '\e[32mTest Successful ready to start!\e[0m'
        else
            echo -e '\e[31mFailed\e[0m'
            echo -e '\e[31m ${ip_list} nginx -t检测失败，请检查配置文件语法\e[0m'
            exit 0
        fi

        ssh root@${ip_list}  "netstat -nutpl | grep nginx" >/dev/null 2>&1
        if [ $? -eq 0 ];then
            ssh root@${ip_list}  "nginx -s reload"
            # echo -e '\e[32m已为您重载nginx配置文件!\e[0m'
        else
            echo -e '\e[31mnginx没有启动，正在为您重新启动nginx\e[0m'
            ssh root@${ip_list}  "nginx"
        fi
   done
}






# ------------------------------------------- 只配置一层负载均衡 ------------------------------------------- #
function Nginx_Proxy01() {
    read -p "[请输入项目名]："  Parameter01
    read -p "[请输入端口号，多个以空格做分隔符]："  PORT_LIST
    read -p "[请输入后端服务器IP地址]："  IP_SERVER
    read -p "[相同配置的备用节点，请输入IP地址]："  IP_BackProxy01

    # 同步脚本配置文件
    scp -r ${ScriptDir}/*  root@${IP_LIST02}:${ScriptDir} >/dev/null 2>&1

    # 检查配置文件是否配置了"include proxy"
    cd /usr/local/nginx/conf/ && grep "include proxy" /usr/local/nginx/conf/nginx.conf >/dev/null 2>&1
    if [ $? -eq 0 ];then
        # 检查是否创建/usr/local/nginx/conf/proxy 目录
        if [ ! -d ${Proxy_ProjectDir} ];then mkdir ${Proxy_ProjectDir}; fi
        ssh root@${IP_BackProxy01} "if [ ! -d ${Proxy_ProjectDir} ];then mkdir ${Proxy_ProjectDir}; fi"
    else
        echo -e '\e[31m请先在nginx配置文件最后一行中定义"include proxy/*.conf并配置日志格式"\e[0m'
        exit 1
    fi

    # 创建多个端口
    VALUES_LIST=$(echo ${PORT_LIST} | wc -w)

    # 检查是否有相同配置
    if [ "${VALUES_LIST}" -eq 1 ];then
        # 检查负载均衡是否有相同配置
        ssh root@${IP_SERVER} "grep ${PORT_LIST} $(ls ${Proxy_ProjectDir}/*)" >/dev/null 2>&1
        if [ $? -eq 0 ];then  echo "${IP_SERVER}：${PORT_LIST}已有相同端口" ; exit ;fi

        ssh root@${IP_BackProxy01} "grep ${PORT_LIST} $(ls ${Proxy_ProjectDir}/*)" >/dev/null 2>&1
        if [ $? -eq 0 ];then  echo "${IP_BackProxy01}：${PORT_LIST}已有相同端口" ; exit ;fi

        # 检查vhost是否有相同配置
        ssh root@${IP_SERVER} "grep ${PORT_LIST} $(ls ${NginxConfig}/vhost/*)" >/dev/null 2>&1
        if [ $? -eq 0 ];then  echo "${IP_SERVER}：${PORT_LIST}已有相同端口" ; exit ;fi

        ssh root@${IP_BackProxy01} "grep ${PORT_LIST} $(ls ${NginxConfig}/vhost/*)" >/dev/null 2>&1
        if [ $? -eq 0 ];then  echo "${IP_BackProxy01}：${PORT_LIST}已有相同端口" ; exit ;fi

    elif [ "${VALUES_LIST}" -ge 2 ];then
        for i in $(seq  ${PORT_LIST})
        do
            # # 检查负载均衡是否有相同配置
            ssh root@${IP_SERVER} "grep ${i} $(ls ${Proxy_ProjectDir}/*)" >/dev/null 2>&1
            if [ $? -eq 0 ];then  echo "${IP_SERVER}：${i}已有相同端口" ; exit ;fi

            ssh root@${IP_BackProxy01} "grep ${i} $(ls ${Proxy_ProjectDir}/*)" >/dev/null 2>&1
            if [ $? -eq 0 ];then  echo "${IP_BackProxy01}：${i}已有相同端口" ; exit ;fi

            # # 检查vhost是否有相同配置
            ssh root@${IP_SERVER} "grep ${i} $(ls ${NginxConfig}/vhost/*)" >/dev/null 2>&1
            if [ $? -eq 0 ];then  echo "${IP_SERVER}：${i}已有相同端口" ; exit ;fi

            ssh root@${IP_BackProxy01} "grep ${i} $(ls ${NginxConfig}/vhost/*)" >/dev/null 2>&1
            if [ $? -eq 0 ];then  echo "${IP_BackProxy01}：${i}已有相同端口" ; exit ;fi
        done
    fi


    # 修改配置文件
    if [ "${VALUES_LIST}" == 1 ];then
        Sed_config01
         echo 1
    elif [ "${VALUES_LIST}" -ge 2 ];then
        for i in $(seq  ${PORT_LIST})
        do
            Sed_config01
        done
    fi

    # 修改备份负载均衡配置文件
    if [ "${VALUES_LIST}" == 1 ];then
        for i in ${PORT_LIST}
        do
            if [ ! ${IP_BackProxy01} ]; then
                echo ""
            else
                Back_proxy01
            fi
        done
    elif [ "${VALUES_LIST}" -ge 2 ];then
        for i in $(seq  ${PORT_LIST})
        do
            if [ ! ${IP_BackProxy01} ]; then
                echo ""
            else
                Back_proxy01
            fi
        done
    fi
}



# ------------------------------------------- 两层负载均衡到服务端 ------------------------------------------- #
function Nginx_Proxy02() {
    read -p "[请输入项目名]："  Parameter01
    read -p "[请输入端口号，多个以空格做分隔符]："  PORT_LIST
    read -p "[请输入第二层负载均衡的IP地址]："  IP_LIST02
    read -p "[请输入后端服务器IP地址]："  IP_SERVER
    read -p "[第一负载，相同配置的备用节点，请输入IP地址]："  IP_BackProxy01
    read -p "[第二负载，相同配置的备用节点，请输入IP地址]："  IP_BackProxy02

    # 同步脚本配置文件
    scp -r ${ScriptDir}/*  root@${IP_LIST02}:${ScriptDir} >/dev/null 2>&1

    cd /usr/local/nginx/conf/ && grep "include proxy" /usr/local/nginx/conf/nginx.conf >/dev/null 2>&1
    if [ $? -eq 0 ];then
        # 检查是否创建/usr/local/nginx/conf/proxy 目录
        if [ ! -d ${Proxy_ProjectDir} ];then mkdir ${Proxy_ProjectDir}; fi
        ssh root@${IP_LIST02} "if [ ! -d ${Proxy_ProjectDir} ];then mkdir ${Proxy_ProjectDir}; fi"
        ssh root@${IP_BackProxy01} "if [ ! -d ${Proxy_ProjectDir} ];then mkdir ${Proxy_ProjectDir}; fi"
        ssh root@${IP_BackProxy02} "if [ ! -d ${Proxy_ProjectDir} ];then mkdir ${Proxy_ProjectDir}; fi"
    else
        echo -e '\e[31m请先在nginx配置文件最后一行中定义"include proxy/*.conf并配置日志格式"\e[0m'
        exit 1
    fi

    # 创建多个端口
    VALUES_LIST=$(echo ${PORT_LIST} | wc -w)

        # 检查是否有相同配置
    if [ "${VALUES_LIST}" -eq 1 ];then
        # 检查负载均衡是否有相同配置
        ssh root@${IP_SERVER} "grep ${PORT_LIST} $(ls ${Proxy_ProjectDir}/*)" >/dev/null 2>&1
        if [ $? -eq 0 ];then  echo "${IP_SERVER}：${PORT_LIST}已有相同端口" ; exit ;fi

        ssh root@${IP_BackProxy01} "grep ${PORT_LIST} $(ls ${Proxy_ProjectDir}/*)" >/dev/null 2>&1
        if [ $? -eq 0 ];then  echo "${IP_BackProxy01}：${PORT_LIST}已有相同端口" ; exit ;fi

        ssh root@${IP_BackProxy02} "grep ${PORT_LIST} $(ls ${Proxy_ProjectDir}/*)" >/dev/null 2>&1
        if [ $? -eq 0 ];then  echo "${IP_BackProxy01}：${PORT_LIST}已有相同端口" ; exit ;fi

        # 检查vhost是否有相同配置
        ssh root@${IP_SERVER} "grep ${PORT_LIST} $(ls ${NginxConfig}/vhost/*)" >/dev/null 2>&1
        if [ $? -eq 0 ];then  echo "${IP_SERVER}：${PORT_LIST}已有相同端口" ; exit ;fi

        ssh root@${IP_BackProxy01} "grep ${PORT_LIST} $(ls ${NginxConfig}/vhost/*)" >/dev/null 2>&1
        if [ $? -eq 0 ];then  echo "${IP_BackProxy01}：${PORT_LIST}已有相同端口" ; exit ;fi

        ssh root@${IP_BackProxy02} "grep ${PORT_LIST} $(ls ${NginxConfig}/vhost/*)" >/dev/null 2>&1
        if [ $? -eq 0 ];then  echo "${IP_BackProxy01}：${PORT_LIST}已有相同端口" ; exit ;fi

    elif [ "${VALUES_LIST}" -ge 2 ];then
        for i in $(seq  ${PORT_LIST})
        do
            # # 检查负载均衡是否有相同配置
            ssh root@${IP_SERVER} "grep ${i} $(ls ${Proxy_ProjectDir}/*)" >/dev/null 2>&1
            if [ $? -eq 0 ];then  echo "${IP_SERVER}：${i}已有相同端口" ; exit ;fi

            ssh root@${IP_BackProxy01} "grep ${i} $(ls ${Proxy_ProjectDir}/*)" >/dev/null 2>&1
            if [ $? -eq 0 ];then  echo "${IP_BackProxy01}：${i}已有相同端口" ; exit ;fi

            ssh root@${IP_BackProxy02} "grep ${i} $(ls ${Proxy_ProjectDir}/*)" >/dev/null 2>&1
            if [ $? -eq 0 ];then  echo "${IP_BackProxy01}：${i}已有相同端口" ; exit ;fi

            # # 检查vhost是否有相同配置
            ssh root@${IP_SERVER} "grep ${i} $(ls ${NginxConfig}/vhost/*)" >/dev/null 2>&1
            if [ $? -eq 0 ];then  echo "${IP_SERVER}：${i}已有相同端口" ; exit ;fi

            ssh root@${IP_BackProxy01} "grep ${i} $(ls ${NginxConfig}/vhost/*)" >/dev/null 2>&1
            if [ $? -eq 0 ];then  echo "${IP_BackProxy01}：${i}已有相同端口" ; exit ;fi

            ssh root@${IP_BackProxy02} "grep ${i} $(ls ${NginxConfig}/vhost/*)" >/dev/null 2>&1
            if [ $? -eq 0 ];then  echo "${IP_BackProxy01}：${i}已有相同端口" ; exit ;fi
        done
    fi

    # 修改配置文件
    if [ "${VALUES_LIST}" == 1 ];then
        for i in ${PORT_LIST}
        do
          Sed_config01
          Sed_config02
        done
    elif [ "${VALUES_LIST}" -ge 2 ];then
        for i in $(seq  ${PORT_LIST})
        do
            Sed_config01
            Sed_config02
        done
    fi

    # 修改备份负载均衡配置文件
    if [ "${VALUES_LIST}" == 1 ];then
        for i in ${PORT_LIST}
        do
            if [ ! ${IP_BackProxy01} ]; then
                echo ""
            else
                Back_proxy01
            fi

            if [ ! ${IP_BackProxy02} ]; then
                echo ""
            else
                Back_proxy02
                
            fi
        done
    elif [ "${VALUES_LIST}" -ge 2 ];then
        for i in  $(seq  ${PORT_LIST})
        do
            if [ ! ${IP_BackProxy01} ]; then
                echo ""
            else
                Back_proxy01
            fi

            if [ ! ${IP_BackProxy02} ]; then
                echo ""
            else
                Back_proxy02
                
            fi
        done
    fi
}



echo "[1：安装nginx]"
echo "[2：配置负载均衡]"
read -p "[请选择1|2]："  initialize
if [ "${initialize}" == '1' ];then
    Install_Nginx
elif [ "${initialize}" == '2' ];then
    echo "[1：一层负载均衡]"
    echo "[2：两层负载均衡]"
    read -p "[请选择1|2]："  proxy_config
    if [ "${proxy_config}" == "1" ];then
        Nginx_Proxy01
    elif [ "${proxy_config}" == "2" ];then
        Nginx_Proxy02
    fi
else
    echo "[请选择 1|2]"
fi

